#include <iostream>
#include <queue>

using namespace std;




void process(int n) {
	priority_queue<int> q;
		
	int number;
	for (int k = 0; k < n; k++) {
		cin >> number;
		number = -number;
		q.push(number);	
	}

	int cost = 0;
	int partialCost = 0;
	int x, y;	
		
	for (int k = 0; k < n - 1; k++) {
		x = q.top();
		q.pop();
		y = q.top();
		q.pop();
		x = -x;
		y = -y;
		partialCost = x + y;
		cost += partialCost;
		q.push(-partialCost);
	}

	cout << cost;
	cout << "\n";
}


int main() {

	int n;

	while (true) {
		cin >> n;
//cout << "n: ";
//cout << n;
//cout << "\n";
		if (n == 0) break;
		process(n);	
	}
}	
